package com.runen.infinitemovement.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.WebActivity;
import com.runen.infinitemovement.adapter.ConsultTitleAdapter;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.ConsultTitleEntity;
import com.runen.infinitemovement.model.entity.SubjectTypeEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class ConsultFragmentV2 extends Fragment {
    @BindView(R.id.rcv_title)
    RecyclerView rcv_title;
    @BindView(R.id.vp_content)
    ViewPager vp_content;
    @BindView(R.id.ll_content)
    RelativeLayout ll_content;
    @BindView(R.id.iv_long_school)
    ImageView iv_long_school;
    @BindView(R.id.ll_body_content)
    LinearLayout ll_body_content;
    private View view;
    private ConsultTitleAdapter adapter;
    private List<ConsultTitleEntity> list;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_consult_v2, container, false);
            ButterKnife.bind(this, view);
//            initView();
            //适配刘海屏
            NotchTools.getFullScreenTools().translucentStatusBar(getActivity(), new OnNotchCallBack() {
                @Override
                public void onNotchPropertyCallback(NotchProperty notchProperty) {
                    ll_content.setPadding(0, CommonUtil.getStatusBarHeight(getActivity()), 0, 0);
                }
            });
            initData();
            iv_long_school.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent3 = new Intent(getActivity(), WebActivity.class);
                    intent3.putExtra("LINK_URL", String.format(Constant.H5_ASK_INDEX, UserAPI.getUserToken()));
                    startActivity(intent3);
                }
            });
        }
        return view;
    }

    private void initData() {
        new ActivityAPI().subjectType(new BaseResultCallback<SubjectTypeEntity>() {
            @Override
            public void onResponse(SubjectTypeEntity response) {
                if (response != null && response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        list = response.data;
                        initView();
                        response.data.get(0).isSelect = true;
                        adapter.setNewInstance(response.data);
                        initSportsTypeName(response.data);
                    }
                }
            }
        });
    }

    private void initView() {
        LinearLayoutManager llm = null;
        if (list.size() > 4) {
            llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        } else
            llm = new GridLayoutManager(getActivity(), list.size());
        adapter = new ConsultTitleAdapter(getActivity(), list.size() > 4, ll_body_content.getY());

        rcv_title.setLayoutManager(llm);
        rcv_title.setAdapter(adapter);

        vp_content.setOffscreenPageLimit(list.size());

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                for (ConsultTitleEntity entity : adapter.getData()) {
                    entity.isSelect = false;
                }
                adapter.getData().get(position).isSelect = true;
                adapter.notifyDataSetChanged();
                vp_content.setCurrentItem(position);
            }
        });
    }

    private void initSportsTypeName(List<ConsultTitleEntity> data) {
        vp_content.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
//                if (position == 0) {
//                    NewsFragment newsFragment = (NewsFragment) NewsFragment.newInstance(position + 1);
//                    return newsFragment;
//                } else {
//                    CollegeFragment newsFragment = (CollegeFragment) CollegeFragment.newInstance(position + 1);
//                    return newsFragment;
//                }
                NewsFragmentV2 newsFragmentV2 = (NewsFragmentV2) NewsFragmentV2.getInstance(data.get(position).id);
                return newsFragmentV2;
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        vp_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (ConsultTitleEntity entity : list) {
                    entity.isSelect = false;
                }
                list.get(position).isSelect = true;
                rcv_title.smoothScrollToPosition(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
